#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

// 学生成绩结构体
struct Student {
    string name;
    string sid;
    string course;
    double score;
};

vector<Student> students; // 存储所有学生成绩

// 记录学生成绩
void recordGrade() {
    Student s;
    cout << "===== 记录学生成绩 =====" << endl;
    cout << "请输入学生姓名：";
    cin >> s.name;
    cout << "请输入学生学号：";
    cin >> s.sid;
    
    // 检查学号是否已存在
    auto it = find_if(students.begin(), students.end(), 
                     [&](const Student& student) { return student.sid == s.sid; });
    if (it != students.end()) {
        cout << "错误：学号已存在！" << endl;
        return;
    }
    
    cout << "请输入课程名称：";
    cin >> s.course;
    
    cout << "请输入成绩（0-100）：";
    cin >> s.score;
    
    // 检查成绩范围
    if (s.score < 0 || s.score > 100) {
        cout << "错误：成绩必须在0-100之间！" << endl;
        return;
    }
    
    students.push_back(s);
    cout << "成绩已成功记录！" << endl;
}

// 查询学生成绩
void queryGrade() {
    cout << "===== 查询学生成绩 =====" << endl;
    cout << "请选择查询方式：" << endl;
    cout << "1. 按学生姓名查询" << endl;
    cout << "2. 按学生学号查询" << endl;
    cout << "3. 按课程名称查询" << endl;
    cout << "请输入选项序号：";
    
    int option;
    cin >> option;
    
    // 检查输入是否为有效选项
    if (option < 1 || option > 3) {
        cout << "错误：无效的选项！" << endl;
        return;
    }
    
    string keyword;
    cout << "请输入查询关键字：";
    cin >> keyword;
    
    vector<Student> results;
    
    // 根据选择的查询方式筛选结果
    switch(option) {
        case 1: // 按姓名查询
            copy_if(students.begin(), students.end(), back_inserter(results),
                   [&](const Student& s) { return s.name == keyword; });
            break;
        case 2: // 按学号查询
            copy_if(students.begin(), students.end(), back_inserter(results),
                   [&](const Student& s) { return s.sid == keyword; });
            break;
        case 3: // 按课程查询
            copy_if(students.begin(), students.end(), back_inserter(results),
                   [&](const Student& s) { return s.course == keyword; });
            break;
    }
    
    // 输出查询结果
    if (results.empty()) {
        cout << "无相关记录！" << endl;
    } else {
        for (const auto& s : results) {
            cout << "姓名：" << s.name 
                 << ", 学号：" << s.sid 
                 << ", 课程：" << s.course 
                 << ", 成绩：" << s.score << endl;
        }
    }
}

// 统计课程成绩
void statCourseGrade() {
    cout << "===== 统计课程成绩 =====" << endl;
    cout << "请输入课程名称：";
    string course;
    cin >> course;
    
    vector<double> scores;
    for (const auto& s : students) {
        if (s.course == course) {
            scores.push_back(s.score);
        }
    }
    
    if (scores.empty()) {
        cout << "无该课程记录！" << endl;
        return;
    }
    
    // 计算统计数据
    double sum = 0;
    double maxScore = scores[0];
    double minScore = scores[0];
    
    for (double score : scores) {
        sum += score;
        if (score > maxScore) maxScore = score;
        if (score < minScore) minScore = score;
    }
    
    double average = sum / scores.size();
    
    // 输出统计结果
    cout << "课程：" << course << endl;
    cout << "平均分：" << average << endl;
    cout << "最高分：" << maxScore << endl;
    cout << "最低分：" << minScore << endl;
}

int main() {
    int choice;
    
    while (true) {
        cout << endl;
        cout << "=================================" << endl;
        cout << "欢迎使用学生成绩管理系统" << endl;
        cout << "=================================" << endl;
        cout << "请选择操作：" << endl;
        cout << "1. 记录学生成绩" << endl;
        cout << "2. 查询学生成绩" << endl;
        cout << "3. 统计课程成绩" << endl;
        cout << "4. 退出系统" << endl;
        cout << "请输入选项序号：";
        
        cin >> choice;
        
        switch(choice) {
            case 1:
                recordGrade();
                break;
            case 2:
                queryGrade();
                break;
            case 3:
                statCourseGrade();
                break;
            case 4:
                cout << "感谢使用学生成绩管理系统，再见！" << endl;
                return 0;
            default:
                cout << "错误：无效的选项，请重新输入！" << endl;
        }
    }
    
    return 0;
}